package com.beiding.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class JsonUtils {

    private static ObjectMapper objectMapper = new ObjectMapper();

    public static String toJson(Object obj) {

        try {
            return objectMapper.writeValueAsString(obj).replace(',','-');
        } catch (JsonProcessingException e) {
            return null;
        }

    }

    public static <T> T fromJson(String json, Class<T> type) {

        try {
            return objectMapper.readValue(json.replace('-', ','), type);
        } catch (IOException e) {
            return null;
        }
    }

}
